namespace chatroom{
    class relationTable{
        public:
            relationTable(std::shared_ptr<odb::core::database>& db):_db(db){}
            //新增关系信息
            bool insert(const std::string& uid,const std::string& pid)
            {
                try{
                    relation r1(uid,pid);
                    relation r2(pid,uid);
                    //开启事务
                    odb::transaction t(_db->begin());
                    _db->persist(r1);
                    _db->persist(r2);
                    t.commit();
                }catch(std::exception& e){
                    Logger_ERROR("往{}-{}添加数据失败,{}",uid,pid,e.what());
                    return false;
                }
                return true;
            }
            //移除用户关系
            bool remove(const std::string& uid,const std::string& pid)
            {
                try{
                   //开启事务
                    odb::transaction t(_db->begin());
                    typedef odb::query<chatroom::relation> query;
                    typedef odb::result<chatroom::relation> result;
                    _db->erase_query<relation>(query::user_id==uid && query::peer_id==pid);
                    _db->erase_query<relation>(query::user_id==pid && query::peer_id==uid);
                    t.commit();
                }catch(std::exception& e){
                    Logger_ERROR("删除指定用户关系{}-{}失败,{}",uid,pid,e.what());
                    return false;
                }
                return true;
            }
            //判断两人是否是好友关系
            bool exist(const std::string& uid,const std::string& pid)
            {
                 try{
                   //开启事务
                    odb::transaction t(_db->begin());
                    typedef odb::query<chatroom::relation> query;
                    typedef odb::result<chatroom::relation> result;
                    auto ret = _db->query<relation>(query::user_id==uid && query::peer_id==pid);
                    t.commit();
                }catch(std::exception& e){
                    Logger_ERROR("判断是否是指定用户{}好友关系失败,{}",uid,e.what());
                    return false;
                }
                return true; 
            }
            //获取指定用户的好友ID
            std::vector<std::string> Get_userID(const std::string& uid)
            {
                std::vector<std::string> res;
                try{
                    //开启事务
                    odb::transaction t(_db->begin());
                    typedef odb::query<chatroom::relation> query;
                    typedef odb::result<chatroom::relation> result;
                    result r(_db->query<chatroom::relation>(query::user_id==uid));
                    for(auto i(r.begin()); i != r.end(); ++i)
                    {
                        res.push_back(i->get_peer_id());
                    }
                    t.commit();
                }catch(std::exception& e){
                    Logger_ERROR("获取会话ID{}中成员失败",ssid,e.what());
                    return res;
                }
                return res;
            }
        private:
            std::shared_ptr<odb::core::database> _db;
    };
}